/**
 * Copyright 2014 lerry
 * 
 * All right reserved.
 * 
 * Dec 9, 2014
 */
package com.bac.bacplatform;



import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * @author lerry
 * 
 */
@SuppressLint("HandlerLeak")
public class AbstructCommonFragment extends Fragment {

	private MyHandler handler = new MyHandler();

	private Dialog mProgressDialog;

	private AlertDialog mDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 进度条
		RelativeLayout rl = (RelativeLayout) LayoutInflater.from(getActivity())
				.inflate(R.layout.loading, null);
		mProgressDialog = new Dialog(getActivity(), R.style.dialog);
		mProgressDialog.getWindow().setContentView(rl);

		// 对话框
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return super.onCreateView(inflater, container, savedInstanceState);

	}
	
	public void showLoadingProgress(){
		sendMessage(MyHandler.SHOW_PROGRESS);
	}
	
	public void dismissLoadingProgress() {
		sendMessageDely(MyHandler.DISMISS_PROGRESS, 600);
	}
	
	public void dismissLoadingProgressNow(){
		sendMessage(MyHandler.DISMISS_PROGRESS);
	}

	protected void handleOtherMessage(int flag) {

	}

	public void sendMessage(int flag) {
		handler.sendEmptyMessage(flag);
	}

	public void sendMessageDely(int flag, long delayMillis) {
		handler.sendEmptyMessageDelayed(flag, delayMillis);
	}

	public void showToast(String toast_message) {
		handler.toast_message = toast_message;
		sendMessage(MyHandler.SHOW_STR_TOAST);
	}

	public void showToast(int res) {
		handler.toast_res = res;
		sendMessage(MyHandler.SHOW_RES_TOAST);
	}

	private class MyHandler extends Handler {
		public static final int SHOW_STR_TOAST = 0;
		public static final int SHOW_RES_TOAST = 1;
		public static final int SHOW_PROGRESS = 2;
		public static final int DISMISS_PROGRESS = 3;
		

		private String toast_message = null;
		private int toast_res;

		@Override
		public void handleMessage(Message msg) {
			if (!Thread.currentThread().isInterrupted()) {
				switch (msg.what) {
				case SHOW_STR_TOAST:
					Toast.makeText(getActivity(), toast_message, Toast.LENGTH_SHORT).show();
					break;
				case SHOW_RES_TOAST:
					Toast.makeText(getActivity(), toast_res, Toast.LENGTH_SHORT).show();
					break;
				case SHOW_PROGRESS:
					if (mProgressDialog != null && !mProgressDialog.isShowing() ) {
						mProgressDialog.show();
					}
					break;
				case DISMISS_PROGRESS:
					if (mProgressDialog != null && mProgressDialog.isShowing()) {
						mProgressDialog.dismiss();
					}
					break;
				default:
					handleOtherMessage(msg.what);
				}
			}
		}

	}

}
